const {getQR,getResult} = require("../modules/bilibili/login/QRlogin")

const playList = require("../modules/kuwo/playList")
const musicList = require("../modules/kuwo/musicList")
const play = require("../modules/kuwo/playUrl")
const search = require("../modules/kuwo/search")
const singer = require("../modules/kuwo/singer")
const album = require("../modules/kuwo/albumInfo")
const rank = require("../modules/kuwo/rank")
const lrc = require("../modules/kuwo/lrc")
var express = require('express');

var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

/**
 * 酷我API···················
 */

/**
 * 歌单推荐
 */
router.get('/list/rank', function(req, res, next) {
  playList.prototype.index({
    order:'hot',
    rn:30,
    pn:1
  }).then((r) => {
    console.log('/list/rank')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});

/**
 * 歌单详情
 */
router.get('/list/detail', function(req, res, next) {
  musicList.prototype.getList({
    pid:req.query.pid,
    pn:1,
    rn:100
  }).then((r) => {
    console.log('/list/detail')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});
/**
 * 音乐播放链接
 */
router.get('/play', function(req, res, next) {
  play.prototype.getPlayUrl({
    mid:req.query.mid,
    type:'music'
  }).then((r) => {
    console.log('/play')
    res.send(r.data)
  }).catch((e) => {
    res.send(e.data)
  })
});

/**
 * 搜索
 */
router.get('/search', function(req, res, next) {
  let type = req.query.type;
  console.log('/search')
  switch (type){
    case '0':
      search.prototype.searchMusicBykeyWord({
        key:encodeURI(req.query.keyword),
        pn : req.query.pn,
        rn : req.query.rn
      }).then((r) => {
        res.send(r.data)
      }).catch((r) => {
        console.log(r.response)
        res.send(JSON.stringify(r))
      });  //单曲
      break;
    case '1':
      search.prototype.searchAlbumBykeyWord({
        key:encodeURI(req.query.keyword),
        pn : 1,
        rn : 30
      }).then((r) => {
        console.log(r.data)
        res.send(r.data)
      }).catch((r) => {
        console.log(r)
      }); //专辑
      break;
    case '2':
      search.prototype.searchPlayListBykeyWord({
        key:encodeURI(req.query.keyword),
        pn : 1,
        rn : 30
      }).then((r) => {
        console.log(r.data)
        res.send(r.data)
      }).catch((r) => {
        console.log(r)
      }); //歌单
      break;
    case '3':
      search.prototype.searchArtistBykeyWord({
        key:encodeURI(req.query.keyword),
        pn : 1,
        rn : 30
      }).then((r) => {
        console.log(r.data)
        res.send(r.data)
      }).catch((r) => {
        console.log(r)
      }); //歌手
      break;
  }


});

/**
 * 歌单详情
 */
router.get('/singer/songs', function(req, res, next) {
  singer.prototype.getArtistAlbum({
    artistid:req.query.id,
    pn:1,
    rn:100
  }).then((r) => {
    console.log('/singer/songs')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});

/**
 * 专辑歌曲
 */
router.get('/album', function(req, res, next) {
  console.log(req.query.id)
  album.prototype.getList({
    albumId:req.query.id,
    pn:1,
    rn:100
  }).then((r) => {
    console.log('/singer/songs')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});
/**
 * 排行榜
 */
router.get('/rank', function(req, res, next) {
  console.log(req.query.id)
  rank.prototype.index().then((r) => {
    console.log('/rank')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});
/**
 * 排行榜歌单详情
 */
router.get('/rank/musicList', function(req, res, next) {
  console.log(req.query.id)
  rank.prototype.getRankMusicList({
    bangId:req.query.bangId,
    pn:req.query.pn || 1,
    rn:req.query.rn || 30
  }).then((r) => {
    console.log('/rank/musicList')
    res.send(r.data)
  }).catch((r) => {
    res.send(r)
  })
});
/**
 * 排行榜歌单详情
 */
router.get('/lrc', function(req, res, next) {
  lrc.prototype.LrcRes(req.query.musicId).then((r) => {
    console.log('/lrc')
    res.send(r.data)
  }).catch((r) => {
   res.send(r)
  })
});



module.exports = router;
